package com.njrsrc.android.app;

import java.util.ArrayList;
import java.util.List;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.njrsrc.R;
import com.njrsrc.android.bean.JobInfo;
import com.njrsrc.android.bean.UserInfo;
import com.njrsrc.android.library.vpi.BaseSampleActivity;
import com.njrsrc.android.tasks.CancelOrCollectOrApplyTask;
import com.njrsrc.android.tasks.JobDetailTask;
import com.njrsrc.android.views.TitleBar;
import com.njrsrc.common.url.RequestUrlProvider;
import com.njrsrc.common.utils.Helpers;
import com.njrsrc.common.utils.MsgID;

/**
 * 工作详情页面，这是一个公共调用的页面，请查看上级页面所需要传入的参数
 * 
 * @see {@link JobCollectActivity}
 * 
 * @author Administrator
 * 
 */
public class JobDetailActivity extends BaseSampleActivity implements OnClickListener {
	private TitleBar mBar;
	private UserInfo userInfo;
	private JobInfo jobInfo;
	private ProgressDialog progressDialog;
	/** 上一个页面需要传过来的工作ID */
	private String job_id;

	// ==============页面控件展示ID==============
	/** 公司名称 */
	private TextView companyName;
	/** 工作名称 */
	private TextView jobName;
	/** 工作地区 */
	private TextView workArea;
	/** 学历 */
	private TextView education;
	/** 工作经验 */
	private TextView workExp;
	/** 工作方式 */
	private TextView workStyle;
	/** 薪资 */
	private TextView salary;
	/** 职位描述 */
	private TextView jobDesc;
	/** 职位要求 */
	private TextView jobNeeds;
	/** 其他描述 */
	private TextView otherDesc;
	/** 发布时间 */
	private TextView publishTime;

	private RelativeLayout job_detail_company_layout;

	/** 是否被收藏 */
	private boolean is_favorite;

	/** 是否已申请过 */
	private boolean is_applied;

	/** 请求地址 */
	private String url;

	private Handler mHandler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			Helpers.closeProgressDialog(progressDialog);
			switch (msg.what) {
			case MsgID.JOB_DETAIL_FAIL:
				// 工作详情获取失败
				Helpers.showToast(JobDetailActivity.this, userInfo.getError_message(), Toast.LENGTH_SHORT);
				break;
			case MsgID.JOB_DETAIL_ERROR:
				// 工作详情获取错误
				Helpers.showToast(JobDetailActivity.this, R.string.connectError, Toast.LENGTH_SHORT);
				break;
			case MsgID.JOB_DETAIL_SUCCESS:
				// 工作详情获取成功
				showView();
				break;

			case MsgID.JOB_ADD_COLLECT_FAIL:
				// 加入收藏失败
				Helpers.showToast(JobDetailActivity.this, userInfo.getError_message(), Toast.LENGTH_SHORT);
				break;

			case MsgID.JOB_ADD_COLLECT_ERROR:
				// 加入收藏错误
				Helpers.showToast(JobDetailActivity.this, R.string.connectError, Toast.LENGTH_SHORT);
				break;

			case MsgID.JOB_ADD_COLLECT_SUCCESS:
				// 加入收藏成功
				Helpers.showToast(JobDetailActivity.this, R.string.addCollectSuccess, Toast.LENGTH_SHORT);
				mBar.setRightOfLeftBg(R.drawable.job_detai_has_collect);
				is_favorite = true;
				break;

			case MsgID.JOB_CANCEL_COLLECT_FAIL:
				// 取消收藏失败
				Helpers.showToast(JobDetailActivity.this, userInfo.getError_message(), Toast.LENGTH_SHORT);
				break;
			case MsgID.JOB_CANCEL_COLLECT_ERROR:
				// 取消收藏错误
				Helpers.showToast(JobDetailActivity.this, R.string.connectError, Toast.LENGTH_SHORT);
				break;

			case MsgID.JOB_CANCEL_COLLECT_SUCCESS:
				// 取消收藏成功
				Helpers.showToast(JobDetailActivity.this, R.string.cancelCollectSuccess, Toast.LENGTH_SHORT);
				mBar.setRightOfLeftBg(R.drawable.job_detai_has_not_collect);
				is_favorite = false;
				break;

			case MsgID.JOB_APPLY_FAIL:
				// 申请工作失败
				Helpers.showToast(JobDetailActivity.this, userInfo.getError_message(), Toast.LENGTH_SHORT);
				break;

			case MsgID.JOB_APPLY_ERROR:
				// 申请工作错误
				Helpers.showToast(JobDetailActivity.this, R.string.connectError, Toast.LENGTH_SHORT);
				break;

			case MsgID.JOB_APPLY_SUCCESS:
				// 申请工作成功
				Helpers.showToast(JobDetailActivity.this, R.string.applySuccess, Toast.LENGTH_SHORT);
				mBar.setRightBg(R.drawable.job_detai_has_apply_job);
				break;

			default:
				break;
			}
			super.handleMessage(msg);
		}
	};

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_job_detail);
		initView();
	}

	private void initView() {
		mBar = (TitleBar) findViewById(R.id.activity_title_bar);
		mBar.setTitleText("工作详情");
		mBar.back.setOnClickListener(this);
		job_id = getIntent().getStringExtra("job_id");
		List<String> params = new ArrayList<String>();
		params.add(job_id);
		params.add(AppApplication.mToken);
		params.add(AppApplication.mUserID);
		String url = Helpers.combinaStr(RequestUrlProvider.JOB_SHOW, params);
		userInfo = new UserInfo();
		jobInfo = new JobInfo();
		Object[] args = new Object[4];
		args[0] = mHandler;
		args[1] = url;
		args[2] = userInfo;
		args[3] = jobInfo;
		progressDialog = Helpers.showProgressDialog(JobDetailActivity.this, R.string.loading);
		new JobDetailTask().execute(args);
	}

	private void showView() {
		companyName = (TextView) findViewById(R.id.job_detail_company_name);
		jobName = (TextView) findViewById(R.id.job_detail_job_name);
		workArea = (TextView) findViewById(R.id.job_detail_area_content);
		education = (TextView) findViewById(R.id.job_detail_edu);
		workExp = (TextView) findViewById(R.id.job_detail_exp);
		workStyle = (TextView) findViewById(R.id.job_detail_style);
		salary = (TextView) findViewById(R.id.job_detail_salary);
		jobDesc = (TextView) findViewById(R.id.job_detail_desc_tv);
		jobNeeds = (TextView) findViewById(R.id.job_detail_deeds_tv);
		otherDesc = (TextView) findViewById(R.id.job_detail_other_tv);
		publishTime = (TextView) findViewById(R.id.job_detail_time_label);

		if (jobInfo.getCompany() != null) {
			String companyNameString = jobInfo.getCompany().getName();
			if (!TextUtils.isEmpty(companyNameString) && !"null".equals(companyNameString)) {
				companyName.setText(companyNameString);
			}
		}
		String titleString = jobInfo.getTitle();
		if (!TextUtils.isEmpty(titleString) && !"null".equals(titleString)) {
			jobName.setText(titleString);
		}
		String areaString = jobInfo.getArea();
		if (!TextUtils.isEmpty(areaString) && !"null".equals(areaString)) {
			workArea.setText(areaString);
		}
		String eduString = jobInfo.getEducation();
		if (!TextUtils.isEmpty(eduString) && !"null".equals(eduString)) {
			education.setText(eduString);
		}
		String workAgeString = jobInfo.getWork_age();
		if (!TextUtils.isEmpty(workAgeString) && !"null".equals(workAgeString)) {
			workExp.setText(workAgeString);
		}
		String workTypeString = jobInfo.getWork_type();
		if (!TextUtils.isEmpty(workTypeString) && !"null".equals(workTypeString)) {
			workStyle.setText(workTypeString);
		}
		String salaryString = jobInfo.getSalary();
		if (!TextUtils.isEmpty(salaryString) && !"null".equals(salaryString)) {
			salary.setText(salaryString);
		}
		String descString = jobInfo.getDescription();
		if (!TextUtils.isEmpty(descString) && !"null".equals(descString)) {
			jobDesc.setText(descString);
		}
		String requirement = jobInfo.getRequirement();
		if (!TextUtils.isEmpty(requirement) && !"null".equals(requirement)) {
			jobNeeds.setText(requirement);
		}
		String otherString = jobInfo.getOther();
		if (!TextUtils.isEmpty(otherString) && !"null".equals(otherString)) {
			otherDesc.setText(otherString);
		}
		String publishTimeString = jobInfo.getModified_at();
		if (publishTimeString != null && !TextUtils.isEmpty(otherString) && !"null".equals(otherString)) {
			publishTime.setText(publishTime.getText() + "    " + Helpers.getStandardTimeStr(this, publishTimeString));
		}
		job_detail_company_layout = (RelativeLayout) findViewById(R.id.job_detail_company_layout);
		job_detail_company_layout.setOnClickListener(this);

		// 右上角图标显示
		if (AppApplication.isLogin) {
			mBar.setTitleLayoutParams();
			// 已登录用户
			is_favorite = jobInfo.isIs_favorite();
			is_applied = jobInfo.isIs_applied();
			if (is_favorite) {
				// 已收藏
				mBar.setRightOfLeftBg(R.drawable.job_detai_has_collect);
			} else {
				mBar.setRightOfLeftBg(R.drawable.job_detai_has_not_collect);
			}
			if (is_applied) {
				// 已申请
				mBar.setRightBg(R.drawable.job_detai_has_apply_job);
			} else {
				mBar.setRightBg(R.drawable.job_detai_has_not_apply_job);
			}
			// 设置点击事件
			mBar.rightOfLeft.setOnClickListener(this);
			mBar.right.setOnClickListener(this);
		}
	}

	@Override
	public void onClick(View v) {
		String type = "";
		switch (v.getId()) {
		case R.id.tb_ibtn_back:
			finish();
			break;
		case R.id.job_detail_company_layout:
			Bundle companyData = new Bundle();
			companyData.putString("companyId", jobInfo.getCompany().getCompany_id());
			
			Intent companyPositionIntent = new Intent();
			companyPositionIntent.setClass(JobDetailActivity.this, CompanyPositionActivity.class);
			companyPositionIntent.putExtras(companyData);
			startActivity(companyPositionIntent);
			break;
		case R.id.tb_ibtn_right_of_left:
			// 收藏
			if (!is_favorite) {
				// 加入收藏
				String id = jobInfo.getJob_id();
				String requestUrl = RequestUrlProvider.JOB_ADD_FAVORITE;
				type = "1";
				runTask(id, requestUrl, type);
			} else {
				// 取消收藏
				String id = jobInfo.getJob_fav_id();
				String requestUrl = RequestUrlProvider.JOB_DELETE_FAVORITE;
				type = "2";
				runTask(id, requestUrl, type);
			}
			break;
		case R.id.tb_ibtn_right:
			// 申请
			if (is_applied) {
				// 提示已经申请过
				Helpers.showToast(JobDetailActivity.this, R.string.jobDetailHasApplied, Toast.LENGTH_SHORT);
			} else {
				// 去申请
				String id = jobInfo.getJob_id();
				String requestUrl = RequestUrlProvider.JOB_APPLY;
				type = "3";
				runTask(id, requestUrl, type);
			}
			break;
		default:
			break;
		}
	}

	/**
	 * 
	 * @param id
	 * @param requestUrl
	 * @param type
	 *            1：加入收藏；2：取消收藏；3：申请工作
	 */
	private void runTask(String id, String requestUrl, String type) {
		List<String> params = new ArrayList<String>();
		params.add(id);
		params.add(AppApplication.mToken);
		params.add(AppApplication.mUserID);
		url = Helpers.combinaStr(requestUrl, params);
		userInfo = new UserInfo();
		Object[] args = new Object[4];
		args[0] = mHandler;
		args[1] = url;
		args[2] = userInfo;
		args[3] = type;
		progressDialog = Helpers.showProgressDialog(JobDetailActivity.this, R.string.loading);
		new CancelOrCollectOrApplyTask().execute(args);
	}
}